<?php
class Item {
	public function add($iUserId, $iCategroyId, $sName, $sImage, $sContent, $aUserPriv = array(), $aGroupPriv = array()) {
		$oDb = Base::loadClass ( 'Db' );
		
		if (count ( $aUserPriv )) {
			$sUserPriv = implode ( ',', $aUserPriv );
		} else {
			$sUserPriv = null;
		}
		if (count ( $aGroupPriv )) {
			$sGroupPriv = implode ( ',', $aGroupPriv );
		} else {
			$sGroupPriv = null;
		}
		$iTime = time ();
		
		$sSql = 'INSERT
					item
				SET
					ite_cat_id = ?,
					ite_name = ?,
					ite_image_path = ?,
					ite_content = ?,
					ite_time = ?,
					ite_use_id = ?,
					ite_user_privilege = ?,
					ite_group_privilege = ?';
		
		$oDb->exec ( $sSql, array ($iCategroyId, $sName, $sImage, $sContent, $iTime, $iUserId, $sUserPriv, $sGroupPriv ) );
		return $oDb->getInsertId ();
	}
	
	public function edit($iId, $sKey, $sValue) {
		$aAlias = array ('name' => 'ite_name' );
		if (array_key_exists ( $sKey, $aAlias )) {
			$oDb = Base::loadClass ( 'Db' );
			$sSql = 'UPDATE item SET ' . $aAlias [$sKey] . ' = ? WHERE ite_id = ?';
			if ($oDb->exec ( $sSql, array ($sValue, $iId ) )) {
				$oCache = Base::loadClass ( 'Cache' );
				$oCache->del ( 1, 'category' );
				return true;
			}
		}
		return false;
	}
	
	public function upload($aFile, $sUploadId, $sType) {
		$oFile = Base::loadClass ( 'File' );
		$sMime = $oFile->getMime ( $aFile ['tmp_name'] );
		if ($sMime == false) {
			return '文件上传失败';
		}
		
		$aMime = Base::getConf ( 'upload.' . $sType );
		if (in_array ( $sMime, $aMime )) {
			list ( $iMico, $iSec ) = explode ( ' ', microtime () );
			$sFiliename = $iSec . substr ( $iMico, 2 ) . '.jpg';
			$oFile->md ( ROOT . '/upload/images/0/' . date ( Ymd ) );
			
			if (move_uploaded_file ( $aFile ['tmp_name'], ROOT . '/upload/images/0/' . date ( Ymd ) . '/' . $sFiliename )) {
				$oDb = Base::loadClass ( 'Db' );
				$sSql = 'INSERT 
							item
						SET
							ite_cat_id = ?,
							ite_name = ?,
							ite_image_path = ?,
							ite_time = ?,
							ite_use_id = ?,
							ite_upload_id = ?,
							ite_Status = ?';
				if ($oDb->exec ( $sSql, array (1, $aFile ['name'], '/upload/images/0/' . date ( Ymd ) . '/' . $sFiliename, time (), 1, $sUploadId, 'close' ) )) {
					$oImage = Base::loadClass ( 'Image' );
					$oImage->open ( ROOT . '/upload/images/0/' . date ( Ymd ) . '/' . $sFiliename );
					$aSize = Base::getConf ( 'image.size' );
					foreach ( $aSize as $iRow ) {
						$oFile->md ( ROOT . '/upload/images/' . $iRow . '/' . date ( Ymd ) );
						$oImage->resize ( ROOT . '/upload/images/' . $iRow . '/' . date ( Ymd ) . '/' . $sFiliename, $iRow, $iRow );
					}
				} else {
					return '文件上传失败';
				}
			}
		} else {
			return '不支持该文件类型';
		}
		
		if ($sType == 'image') {
			return '<img src="./upload/images/80/' . date ( Ymd ) . '/' . $sFiliename . '" width="40" height="40" border="0" />' . $aFile ['name'];
		} else {
			return $aFile ['name'] . '(' . $sMime . ')';
		}
	}
	
	public function addImage($aUploadId) {
		$oDb = Base::loadClass ( 'Db' );
		foreach ( $aUploadId as $sRow ) {
			$sSql = 'UPDATE item SET ite_status = ? WHERE ite_upload_id = ?';
			$oDb->exec ( $sSql, array ('active', $sRow ) );
		}
		$oCache = Base::loadClass ( 'Cache' );
		$oCache->del ( 1, 'category' );
	}
}
?>